/**
   *
   * @param {缓存key} key
   * @param {需要存储的缓存值} value
   * @param {过期时间, 单位为秒, 默认0表示永久有效} expire
   */

const name = 'ZY_App_'
export const cookie = {
  set(key, value, expire = 0) {
    let obj = {
      data: value, //存储的数据
      time: Date.now() / 1000, //记录存储的时间戳
      expire: expire //记录过期时间，单位秒
    }
    uni.setStorageSync(name + key, JSON.stringify(obj))
  },
  get(key) {
    let cacheVal = uni.getStorageSync(name + key)
    if (!cacheVal) {
      return null
    }
    cacheVal = JSON.parse(cacheVal)
    if (cacheVal.expire && Date.now() / 1000 - cacheVal.time > cacheVal.expire) {
      uni.removeStorageSync(name + key)
      return null
    }
    return cacheVal.data
  },
  remove(key) {
    if (key) {
      console.log('keykeykeykeykey',key)
      uni.removeStorageSync(name + key)
    } else {
      uni.clearStorageSync()
    }
  }
}
